package cn.tony.game.super_tank;

import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import cn.tony.game.super_tank.msg.CloseMessage;
import cn.tony.game.super_tank.msg.TankMoveMessage;
import cn.tony.game.super_tank.msg.ZidanMessage;

public class Game {

    public static final int          H          = 768;
    public static final int          W          = 1024;
    public static final int          AICOUNT    = 8;
    public static final int          TANK_SPEED = 2;
    public static final int          TANK_CELL  = 40;
    public static final int          BOOMLIFE   = 20;

    public static final Color        black      = Color.black;
    public static final int          BOOMD      = 1;
    public static Map<Integer, Tank> list       = new HashMap<>();

    public Tank                      player     = null;

    MainView                         mv;
    WelcomeView                      welcome;
    JFrame                           frame;

    public volatile List<Drawable>   ds         = new ArrayList<Drawable>();
    public int                       id;

    public Socket                    socket;

    public static void main(String[] args) {
        Game game = new Game();
        game.start();
    }

    public void start() {
        mv = new MainView(ds);
        welcome = new WelcomeView();
        frame = new JFrame("SUPER TANK GAME 内测版");
        frame.setBounds(400, 300, W, H);
        frame.setResizable(false);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                try {
                    System.out.println("closing");
                CloseMessage cm = new CloseMessage();
                cm.buildMsg(Game.this).send(socket);
                }catch (Exception e1) {
                    e1.printStackTrace();
                }
                super.windowClosing(e);
            }
            
        });
        
        frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                super.keyPressed(e);
                int keyCode = e.getKeyCode();
                boolean flag = true;
                regMoveKeyListener(keyCode, flag);
            }

            private void regMoveKeyListener(int keyCode, boolean flag) {
                switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    player.left(flag);
                    break;
                case KeyEvent.VK_UP:
                    player.up(flag);
                    break;
                case KeyEvent.VK_RIGHT:
                    player.right(flag);
                    break;
                case KeyEvent.VK_DOWN:
                    player.down(flag);
                    break;
                default:
                    break;
                }
                try {
                TankMoveMessage jm = new TankMoveMessage();
                jm.buildMsg(Game.this).send(socket);
                }catch (Exception e) {
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                super.keyReleased(e);
                int keyCode = e.getKeyCode();
                boolean flag = false;

                if (keyCode == KeyEvent.VK_A) {
                    player.trigger();
                    ZidanMessage zm = new ZidanMessage();
                    zm.buildMsg(Game.this).send(socket);
                } else {
                    regMoveKeyListener(keyCode, flag);
                }
            }
        });

        frame.add(mv);

        // new Thread() {
        // public void run() {
        // try {
        // sleep(3000);
        // } catch (InterruptedException e) {
        // }
        //
        // frame.remove(welcome);
        // frame.add(mv);
        // };
        // }.start();

        frame.setVisible(true);
    }

    public void addPlay(boolean flag, String name, int id) {
        Tank p = new Tank(name, id);
        p.isPlayer = flag;
        ds.add(player);
        list.put(id, p);
    }

    public void addself(boolean b) {
        player.isPlayer = true;
        player.team = id;
        ds.add(player);
        if (!b) {
            player.x = W - player.x;
            join();
        }
    }

    public void join() {

    }

    // private List<Drawable> createGame() {
    // player.isPlayer = true;
    // player.team = true;
    //
    // ds.add(player);
    //
    // for (int i = 0; i < AICOUNT; i++) {
    // Tank tank = new Tank("电脑" + i);
    // tank.x = Game.W - 50;
    // tank.y = i * 60 + 60;
    // tank.dir = 0b1000;
    //
    // tank.team = false;
    // tank.isPlayer = false;
    // ds.add(tank);
    // }
    // return ds;
    // }

    public void addself(int id, String name, int x, int y, int dir, int currentBlood) {
        if (player != null) {
            return;
        }
        this.player = new Tank("");
        Tank player = this.player;

        add(id, name, x, y, dir, currentBlood, player);
    }

    public void addTank(int id, String name, int x, int y, int dir, int currentBlood) {
        Tank player = new Tank("");
        add(id, name, x, y, dir, currentBlood, player);
    }

    private void add(int id, String name, int x, int y, int dir, int currentBlood, Tank player) {
        player.id = id;
        player.team = id;
        player.name = new NameTitle(name);
        player.isPlayer = true;
        player.x = x;
        player.y = y;
        player.oDir = dir;
        player.setBloodStrip(currentBlood);
        ds.add(player);
    }

    public boolean extis(int id2) {
        synchronized (ds) {
            for (int i = 0; i < ds.size(); i++) {
                if (ds.get(i) instanceof Tank) {
                    if (((Tank) ds.get(i)).id == id2) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    public Tank updateTank(int id, String name, int x, int y, int dir, int currentBlood) {
        for (int i = 0; i < ds.size(); i++) {
            if (ds.get(i) instanceof Tank) {
                if (((Tank) ds.get(i)).id == id) {
                    System.out.println("id : " + id + " 更新");
                    Tank player1 = ((Tank) ds.get(i));
                    // player1.id = id;
                    // player1.name = new NameTitle(name);
                    // player1.isPlayer = true;
                    player1.x = x;
                    player1.y = y;
                    // player1.oDir = dir;
                    player1.dir = dir;

                    // player1.bloodStrip = new BloodStrip();
                    // player1.bloodStrip.setCurrentBlood(currentBlood);
                    return player1;
                }
            }
        }

        return null;
    }
    
    public Tank updateTankDie(int id) {
        for (int i = 0; i < ds.size(); i++) {
            if (ds.get(i) instanceof Tank) {
                if (((Tank) ds.get(i)).id == id) {
                    System.out.println("id : " + id + " 更新");
                    Tank player1 = ((Tank) ds.get(i));
                    player1.isAlive=false;
                    return player1;
                }
            }
        }

        return null;
    }
    

}
